import cocos, pyglet
from cocos.actions import *
from cocos.director import director

import locale
locale.setlocale(locale.LC_ALL, "")


import backgrounds, models, message, gui
from config import current as conf

class CityEditorLayer(cocos.layer.Layer):
    """Displays a simple GUI for adjusting parameters of a single city"""
    def __init__(self, city):
        super(CityEditorLayer, self).__init__()
        self.city = city
        print city

        self.add(gui.ControlCatcherLayer(), z = -1)

        # add the translucent background layer
        self.bg = cocos.layer.ColorLayer(0,0,0,0)
        self.add(self.bg)

        # add the image
        self.image = cocos.sprite.Sprite(
            'city-live.png' if city.status == models.City.CLEAN else 'city-dead.png')
        self.image.anchor_y = 'top'
        self.image.anchor_x = 'right'
        self.image.scale = 0.4
        self.image.position = (190, 430)
        self.add(self.image)

        # add the city name
        self.name_label = cocos.text.Label(city.name,
            font_name=conf.title_font, font_size=72,
            color=(60, 0, 0, 255), x=280, y=500,
            anchor_x='left', anchor_y='top')
        self.add(self.name_label, z=1)

        # add the population count label
        self.population_label = cocos.text.Label("",
            font_name=conf.body_font, font_size=14,
            color=(255, 255, 255, 255), x=280, y=400,
            anchor_x='left', anchor_y='top')
        self.add(self.population_label, z=1)

        # add gauges for all adjustable properties
        y = 200
        x = 300
        SPACING = 40

        label_args = dict(
            font_name=conf.body_font, font_size=14, color=(255, 255, 255, 255),
            anchor_x='right', anchor_y='bottom', x=x-10)

        self.scavenging_gauge = gui.Gauge(
            incr = lambda: (city.scavenging.incr(), self.update()),
            decr = lambda: (city.scavenging.decr(), self.update()),
            value = lambda: city.scavenging.value(),
            position = (x,y))
        self.add(self.scavenging_gauge, z=2)
        self.add(cocos.text.Label("Scavenging", y=y+3, **label_args), z=2)
        y += SPACING

        self.research_gauge = gui.Gauge(
            incr = lambda: (city.research.incr(), self.update()),
            decr = lambda: (city.research.decr(), self.update()),
            value = lambda: city.research.value(),
            position = (x,y))
        self.add(self.research_gauge, z=2)
        self.add(cocos.text.Label("Research", y=y+3, **label_args), z=2)
        y += SPACING
        
        self.defense_gauge = gui.Gauge(
            incr = lambda: (city.defense.incr(), self.update()),
            decr = lambda: (city.defense.decr(), self.update()),
            value = lambda: city.defense.value(),
            position = (x,y))
        self.add(self.defense_gauge, z=2)
        self.add(cocos.text.Label("Defense", y=y+3, **label_args), z=2)
        y += SPACING

        self.idle_gauge = gui.Gauge(value = lambda: city.idle_percentage, position = (x,y), adjustable=False)
        self.add(self.idle_gauge, z=2)
        self.add(cocos.text.Label("Idle", y=y+3, **label_args), z=2)
        y += SPACING

        # add a back button
        self.add(gui.ClickableLabel("DONE", self.exit, (500, 30), 36), z=3)

        # add a help button
        self.add(gui.ClickableLabel("HELP", lambda:self.parent.messages.show("Idle: breed faster, defense: kill zombies, researchers: find the cure, scavengers: rebuild walls"), (650, 30), 36), z = 3)

        self.update()        
        self.enter()
    
    def enter(self):

        self.scale = 3.0
        self.bg.do(FadeTo(200, conf.short_transition_time))
        self.do(AccelDeccel(ScaleTo(1.0, conf.short_transition_time)))

    def exit(self):
        self.bg.do(FadeOut(conf.short_transition_time))
        self.do(Accelerate(ScaleTo(3.0, conf.short_transition_time)) + CallFuncS(self.parent.remove))


    def update(self):
        self.scavenging_gauge.update()
        self.research_gauge.update()
        self.idle_gauge.update()
        self.population_label.element.text = locale.format_string("Pop. %d", self.city.population, True)

    is_event_handler = True 
    def on_key_press (self, k, modifiers):
        """Exit on key press"""
        if k == pyglet.window.key.ESCAPE:
            self.exit()
            return True